package cn;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDAOImpl implements IUserDAO {
    private Connection conn = null;

    public UserDAOImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean findLogin(String userid, String password) throws Exception {
        boolean flag = false;
        String sql = "SELECT * FROM user WHERE userid=? AND password=? ";
        PreparedStatement pstmt = this.conn.prepareStatement(sql);
        pstmt.setString(1, userid);
        pstmt.setString(2, password);
        ResultSet rs = pstmt.executeQuery();
        if (rs.next()) {
            flag = true;
        }
        pstmt.close();
        return flag;
    }
    @Override
    public int registerOneUser(String userid,String password,String trueName) throws SQLException {
        String sql = "INSERT INTO user(userid,password,name) VALUES(?,?,?) ";
        PreparedStatement preparedStatement=this.conn.prepareStatement(sql);
        preparedStatement.setString(1,userid);
        preparedStatement.setString(2,password);
        preparedStatement.setString(3,trueName);
        return preparedStatement.executeUpdate();
    }

  /*  @Override
    public List<Userid> findSomeUser(String Name) throws SQLException {
        String sql="SELECT * FROM info WHERE Sno=?";
        PreparedStatement pstmt=this.conn.prepareStatement(sql);
        pstmt.setString(1,Name);
        ResultSet rs=pstmt.executeQuery();
        List<Userid> userList=new ArrayList<>();
        while(rs.next()){
            userList.add(new Userid(rs.getString(1),rs.getString(2),
                    rs.getString(3),rs.getString(4),rs.getString(5)
                    ,rs.getString(6),rs.getString(7)  ));
        }
        pstmt.close();
        System.out.println(userList.size());
        return userList;
    }

*/

    @Override
    public List<Userid> findSomeUser(String userid) throws SQLException {
        String sql="SELECT * FROM user WHERE userid=?";
        PreparedStatement pstmt=this.conn.prepareStatement(sql);
        pstmt.setString(1,userid);
        ResultSet rs=pstmt.executeQuery();
        List<Userid> userList=new ArrayList<>();
        while(rs.next()){
            userList.add(new Userid(rs.getInt(1),rs.getString(2),rs.getString(3),
                    rs.getString(4),rs.getString(5), rs.getString(6),
                    rs.getString(7),rs.getInt(8)));
        }
        pstmt.close();
        System.out.println(userList.size());
        return userList;
    }


    @Override
    public List<Booker> findSomeBooks(String bookname) throws SQLException {
        String sql="SELECT * FROM book WHERE bookname=?";
        PreparedStatement pstmt=this.conn.prepareStatement(sql);
        pstmt.setString(1,bookname);
        ResultSet rs=pstmt.executeQuery();
        List<Booker> userList=new ArrayList<>();
        while(rs.next()){
            userList.add(new Booker(rs.getInt(1),rs.getString(2),
                    rs.getString(3),rs.getString(4),rs.getString(5),rs.getInt(6)));
        }
        pstmt.close();
        System.out.println(userList.size());
        return userList;
    }
}